/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "stdafx.h"
#include <windows.h>
#include "../JlinkUSB/JlinkUSB.h"
#include "../JlinkUSB/JlinkProtocol.h"

#ifdef _DEBUG
#pragma comment (lib, "../debug/jlinkusb.lib")
#else
#pragma comment (lib, "../release/jlinkusb.lib")
#endif


static void read_pin_status(jlink_usb_t *pdev)
{
	int retv;
	jlink_state_t state;

	retv = jlinkusb_get_state(pdev, &state);
	if (retv != 0)
	{
		printf("faild to get state.\n");
		return;
	}
	printf("state.voltage: %d\n", state.voltage);
	printf("state.tck: %d\n", state.tck);
	printf("state.tdi: %d\n", state.tdi);
	printf("state.tdo: %d\n", state.tdo);
	printf("state.tms: %d\n", state.tms);
	printf("state.tres: %d\n", state.tres);
	printf("state.trst: %d\n", state.trst);
}

int test_JlinkUSB(int dev)
{
	int retv;
	jlink_usb_t *pdev;

	pdev = jlinkusb_open(dev);
	if (pdev == NULL)
	{
		printf("faild to open Jlink...\n");
		return -1;
	}

	// EMU_CMD_VERSION
	char version[0x100];
	retv = jlinkusb_get_version(pdev, version);
	if (retv != 0)
	{
		printf("faild to get version.\n");
		goto _out;
	}
	printf("version: %s\n", version);

	// EMU_CMD_GET_SPEEDS
	jlink_speed_t speed;
	retv = jlinkusb_get_speed(pdev, &speed);
	if (retv != 0)
	{
		printf("faild to get speed.\n");
		goto _out;
	}
	printf("speed: %d, %d\n", speed.BaseFreq, speed.MinDiv);
	// EMU_CMD_SET_SPEED
	retv = jlinkusb_set_speed(pdev, 0xBB8);	// 3MHz
	if (retv != 0)
	{
		printf("faild to Clears TMS.\n");
		goto _out;
	}
	printf("set speed: 3MHz\n");
	retv = jlinkusb_get_speed(pdev, &speed);
	if (retv != 0)
	{
		printf("faild to get speed.\n");
		goto _out;
	}
	printf("return speed: %d, %d\n", speed.BaseFreq, speed.MinDiv);


	// EMU_CMD_GET_MAX_MEM_BLOCK
	uint32_t maxblock = 0;
	retv = jlinkusb_get_max_mem_block(pdev, &maxblock);
	if (retv != 0)
	{
		printf("faild to get max mem block.\n");
		goto _out;
	}
	printf("max mem block: %d\n", maxblock);

	// EMU_CMD_GET_CAPS
	jlink_caps_t caps;
	retv = jlinkusb_get_caps(pdev, &caps);
	if (retv != 0)
	{
		printf("faild to get caps.\n");
		goto _out;
	}
	printf("caps: 0x%08x\n", caps.caps);
	// EMU_CMD_GET_CAPS_EX
	retv = jlinkusb_get_caps_ex(pdev, &caps);
	if (retv != 0)
	{
		printf("faild to get caps.\n");
		goto _out;
	}
	printf("caps ex: 0x%08x\n", caps.caps);
	printf("caps ex b[0]: 0x%02x\n", caps.capsex[0]);

	// EMU_CMD_GET_UNKNOW_E6
	jlink_unknow_e6_t ue6;
	retv = jlinkusb_get_unknow_e6(pdev, &ue6);
	if (retv != 0)
	{
		printf("faild to get unknow E6.\n");
		goto _out;
	}
	printf("unknow E6: %s\n", ue6.supp);

	// EMU_CMD_GET_UNKNOW_EB
	jlink_unknow_eb_t ueb;
	retv = jlinkusb_get_unknow_eb(pdev, &ueb);
	if (retv != 0)
	{
		printf("faild to get unknow EB.\n");
		goto _out;
	}
	// EMU_CMD_GET_UNKNOW_C6
	jlink_unknow_c6_t uc6;
	retv = jlinkusb_get_unknow_c6(pdev, &uc6);
	if (retv != 0)
	{
		printf("faild to get unknow EB.\n");
		goto _out;
	}
	// EMU_CMD_GET_UNKNOW_09
	jlink_unknow_09_t u09;
	retv = jlinkusb_get_unknow_09(pdev, &u09, 0);
	if (retv != 0)
	{
		printf("faild to get unknow 09.\n");
		goto _out;
	}
	retv = jlinkusb_get_unknow_09(pdev, &u09, 1);
	if (retv != 0)
	{
		printf("faild to get unknow 09.\n");
		goto _out;
	}

	// EMU_CMD_GET_HW_INFO
	uint32_t hwver = 0;
	retv = jlinkusb_get_hw_version(pdev, &hwver);
	if (retv != 0)
	{
		printf("faild to get hw info.\n");
		goto _out;
	}
	printf("hw version: 0x%08x\n", hwver);

	// EMU_CMD_GET_STATE
	read_pin_status(pdev);

	// EMU_CMD_GET_HW_INFO
	jlink_hwinfo_t hwinfo;
	retv = jlinkusb_get_hw_info(pdev, &hwinfo);
	if (retv != 0)
	{
		printf("faild to get hw info.\n");
		goto _out;
	}
	printf("hwinfo.power_enabled: %d\n", hwinfo.power_enabled);
	printf("hwinfo.power_overcurrent: %d\n", hwinfo.power_overcurrent);
	printf("hwinfo.itarget: %d\n", hwinfo.itarget);
	printf("hwinfo.itarget_peak: %d\n", hwinfo.itarget_peak);

	// EMU_CMD_GET_COUNTERS
	jlink_counters_t hcounters;
	retv = jlinkusb_get_counters(pdev, &hcounters);
	if (retv != 0)
	{
		printf("faild to get counters.\n");
		goto _out;
	}
	printf("hcounters.power_on: %d\n", hcounters.power_on);
	printf("hcounters.power_change: %d\n", hcounters.power_change);

	// EMU_CMD_MEASURE_RTCK_REACT
	jlink_rtck_t rtck;
	retv = jlinkusb_measure_rtck_react(pdev, &rtck);
	if (retv != 0)
	{
		printf("faild to measure rtck react.\n");
		goto _out;
	}
	printf("rtck.result: %d\n", rtck.result);
	printf("rtck.minimum: %d\n", rtck.minimum);
	printf("rtck.maximum: %d\n", rtck.maximum);
	printf("rtck.average: %d\n", rtck.average);

	// EMU_CMD_READ_CONFIG
	jlink_config_t config;
	retv = jlinkusb_read_config(pdev, &config);
	if (retv != 0)
	{
		printf("faild to read config.\n");
		goto _out;
	}
	printf("config.usb_addr: %d\n", config.usb_addr);
	printf("config.ks_power: %d\n", config.ks_power);
	printf("config.ip_addr: 0x%08x\n", config.ip_addr);
	printf("config.ip_mask: 0x%08x\n", config.ip_mask);
	printf("config.mac_addr: 0x%08x\n", config.mac_addr);

	// EMU_CMD_HW_TMS0
	retv = jlinkusb_hw_tms0(pdev);
	if (retv != 0)
	{
		printf("faild to Clears TMS.\n");
		goto _out;
	}
	printf("Clears TMS...\n");
	read_pin_status(pdev);
	// EMU_CMD_HW_TMS1
	retv = jlinkusb_hw_tms1(pdev);
	if (retv != 0)
	{
		printf("faild to Sets TMS.\n");
		goto _out;
	}
	printf("Sets TMS...\n");
	read_pin_status(pdev);

	// EMU_CMD_HW_DATA0
	retv = jlinkusb_hw_data0(pdev);
	if (retv != 0)
	{
		printf("faild to Clears TDI.\n");
		goto _out;
	}
	printf("Clears TDI...\n");
	read_pin_status(pdev);
	// EMU_CMD_HW_DATA1
	retv = jlinkusb_hw_data1(pdev);
	if (retv != 0)
	{
		printf("faild to Sets TDI.\n");
		goto _out;
	}
	printf("Sets TDI...\n");
	read_pin_status(pdev);


_out:
	jlinkusb_close(pdev);

	return 0;
}

